import java.io.*;
import java.util.*;

/**
 * @description: 2016北航991.8分数统计
 * @author: yuanjianpeng5
 * @createdate: 2020-02-16 22:46
 * @lastdate:
 */

public class ScoreTest {

    //    FileSystemResource resource = new FileSystemResource("src/test/resources/test.txt");
//    final File file = resource.getFile();
//
//    ClassPathResource classPathResource = new ClassPathResource("test.txt");

    public static void main(String[] args) throws IOException {
        String realPath = ScoreTest.class.getClassLoader().getResource("").getFile();
        File scorelistFile = new File(realPath,"scorelist.txt");
        FileInputStream fip = null;
        BufferedReader bufferedReader = null;
        Map<String, Integer> scoremap = new HashMap<>();
        try {
            fip = new FileInputStream(scorelistFile);
            bufferedReader = new BufferedReader(new InputStreamReader(fip, "UTF-8"));
            String line; // 第一行是标头 不要
            for(line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
                String[] text = line.split("\t");
                scoremap.put(text[0], Integer.parseInt(text[1]));
            }

            int sumScore = scoremap.values().stream().mapToInt(e -> e).sum();
            System.out.println("平均分是" + sumScore/scoremap.size());

            List<Map.Entry<String, Integer>> list = new LinkedList<>(scoremap.entrySet());
            Collections.sort(list, (o1, o2) -> {
                int compare = (o1.getValue()).compareTo(o2.getValue());
                return -compare;
            });

            System.out.println("排名前三的同学成绩是：");
            for (int i = 0; i < 3 && i < list.size(); i++) {
                Map.Entry<String, Integer> entry = list.get(i);
                System.out.println(entry.getKey() + "/t" + entry.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (fip != null) {
                fip.close();
            }
        }
    }
}